package com.ctgu.tengyun.service.saleorder.impl;

import com.ctgu.tengyun.dao.mybatis.SaleOrderProductRecordMapper;
import com.ctgu.tengyun.domain.SaleOrderProductRecord;
import com.ctgu.tengyun.domain.SaleOrderProductRecordExample;
import com.ctgu.tengyun.service.saleorder.SaleOrderProductRecordService;
import com.ctgu.tengyun.util.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * Created by liaoweizhi on 2019/8/8.
 */
@Service
public class SaleOrderProductRecordServiceImpl implements SaleOrderProductRecordService {

    @Resource
    private SaleOrderProductRecordMapper mapper;

    @Override
    public int insertSelective(SaleOrderProductRecord record) {
        return mapper.insertSelective(record);
    }

    @Override
    public Integer countSaleProduct(String productNo, Long saleDateStart, Long saleDateEnd) {
        SaleOrderProductRecordExample example = new SaleOrderProductRecordExample();
        SaleOrderProductRecordExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotEmpty(productNo)) {
            criteria.andProductNoEqualTo(productNo);
        }
        if (null != saleDateStart) {
            criteria.andSaleDateGreaterThanOrEqualTo(new Date(saleDateStart));
        }
        if (null != saleDateEnd) {
            criteria.andSaleDateLessThanOrEqualTo(new Date(saleDateEnd));
        }

        return mapper.countByExample(example);
    }

    @Override
    public List<SaleOrderProductRecord> selectSaleProductList(String productNo, Long saleDateStart, Long saleDateEnd,
                                                              Integer pageNo, Integer pageSize) {
        SaleOrderProductRecordExample example = new SaleOrderProductRecordExample();
        SaleOrderProductRecordExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotEmpty(productNo)) {
            criteria.andProductNoEqualTo(productNo);
        }
        if (null != saleDateStart) {
            criteria.andSaleDateGreaterThanOrEqualTo(new Date(saleDateStart));
        }
        if (null != saleDateEnd) {
            criteria.andSaleDateLessThanOrEqualTo(new Date(saleDateEnd));
        }

        example.setStartIndex((pageNo - 1) * pageSize);
        example.setPageSize(pageSize);

        return mapper.selectByExample(example);
    }

    @Override
    public SaleOrderProductRecord selectByPrimaryKey(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(SaleOrderProductRecord record) {
        return mapper.updateByPrimaryKeySelective(record);
    }
}
